from nodetypes.formats.mml.Events import EventHandler
from nodetypes.core.Preferences import UrlUserPreferencesUpdater
from myelin.server.lib.http import Status

from forms import Commit
    

class EventHandler(EventHandler):
    
    newPreferencesUpdater       = UrlUserPreferencesUpdater
    newCommitForm               = Commit.Form   
        
    def process(self, context, source):
        
        if self.newPreferencesUpdater().doUpdate(context, source):
            eventData = context.event.data
            eventData.status = u'success'
            eventData.status_type = u'commit-success'
        else:
            eventData = context.event.data
            eventData.status = u'failure'
            eventData.status_type = u'commit-param-errors'
            
            context.request.status = Status.BAD_REQUEST
            
        eventData.form = self.newCommitForm()
        
        eventData.stage = 0
        eventData.submits = context.transform.submitEvents[0]
        
        context.model = context.nodeType.retrieveModel(context)
        
        return self.newDocument().createDocument(context, source)